<?php
exit(0);

include('IXR_Library.inc.php');

$h = fopen(getcwd().'/all-log.txt','a+');
$debugstr = '['.date('H:i:s').']::POST: '.$HTTP_RAW_POST_DATA."\n GET: ".print_r($_GET,true)." FILES:".print_r($_FILES,true)." \n\n";

fwrite($h, $debugstr );


$dbConn = mysql_connect ("localhost", "mup_stest", "~^m3D.nr%WK4") or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db("mup_shop") or die('Cannot select database. ' . mysql_error());
mysql_query("SET NAMES utf8");



function dbQuery($sql){	
	$h = fopen(getcwd().'/log.txt','a+');
	fwrite($h, $sql."\n");
dump($sql);
$result = mysql_query($sql) or die(mysql_error());
	return $result;
}




function dump($value='') 
{
$fp = fopen("xmlrpc.log", 'a');
fwrite($fp, $_SERVER['REMOTE_ADDR'] . " "  . " " . $value."\r\n");
fclose($fp);
} 

function mysql_resultTo2DAssocArray ( $result) {
   $i=0;
   $ret = array();
   while ($row = mysql_fetch_assoc($result)) {
       foreach ($row as $key => $value) {
           $ret[$i][$key] = $value;
           }
       $i++;
       }
   return ($ret);
}


function getSQL($args) {
dump($args); 
//return   mysql_resultTo2DAssocArray( dbQuery($args));
}

function getVersion() {
return   mysql_resultTo2DAssocArray( dbQuery(" SELECT * FROM `SC_system` LIMIT 0 , 30 "));
}

function getPayments($args) {
return   mysql_resultTo2DAssocArray( dbQuery("SELECT *  FROM `SC_orders` WHERE order_time>='" . $args[0]. "' AND order_time<='" . $args[1]. "'"));
}

function getCategory($args) {
dump("SELECT *  FROM `SC_categories` WHERE SC_categories.parent=" . $args);
return   mysql_resultTo2DAssocArray( dbQuery("SELECT *  FROM `SC_categories` WHERE SC_categories.parent=" . $args));
}

function getCategoryNumber($args) {
dump("SELECT *  FROM `SC_categories` WHERE SC_categories.categoryID=" . $args);
return   mysql_resultTo2DAssocArray( dbQuery("SELECT *  FROM `SC_categories` WHERE SC_categories.categoryID=" . $args));
}

function setCategoryNumber($args) {
dbQuery("UPDATE `SC_categories` SET name_ru='" . $args[1] .  "' ,description_ru='" . $args[2] .  "' WHERE categoryID='" . $args[0]. "'");
}


function getProducts($args) {
return   mysql_resultTo2DAssocArray( dbQuery("SELECT *  FROM `SC_products` WHERE categoryID='" . $args. "'"));
}

function getProductNumber($args) {
return   mysql_resultTo2DAssocArray( dbQuery("SELECT *  FROM `SC_products` WHERE productID='" . $args. "'"));
}

function setProductNumber($args) {
dbQuery("UPDATE `SC_products` SET name_ru='" . $args[1] .  "', description_ru='" . $args[2] . "' WHERE productID='" . $args[0]. "'");
}

function getPaymentsNumber($args) {
return   mysql_resultTo2DAssocArray( dbQuery("SELECT *  FROM `SC_ordered_carts` WHERE orderID='" . $args. "'"));
}


function getNews() {
return   mysql_resultTo2DAssocArray( dbQuery("SELECT *  FROM `SC_news_table` "));
}

function setNewsNumber($args) {
if ($args[0] == 0) {	
dbQuery("INSERT `SC_news_table`  (`NID` ,`add_date` ,`title` ,`picture` ,`textToPublication` ,`textToMail` ,`add_stamp` ,`priority` ,`emailed`) VALUES ( NULL , NULL , '" . $args[1] ."', '" . $args[2] ."', '". $args[3]."', '". $args[4]."' , NULL , NULL , NULL)");

} else
{
dbQuery("UPDATE `SC_news_table` SET title='" . $args[1] .  "' , picture='" . $args[2] . "', textToPublication='" . $args[3] . "', textToMail='" . $args[4] . "' WHERE NID='" . $args[0]. "'");}
}

function delNews($args) {
dbQuery("DELETE  FROM `SC_news_table` WHERE NID='" . $args . "'");
return true;
}


function getAuxPage($args) {
return   mysql_resultTo2DAssocArray( dbQuery("SELECT *  FROM `SC_aux_pages` "));
}

function setAuxPageNumber($args) {
if ($args[0] == 0) {
dbQuery("INSERT `SC_aux_pages` (`aux_page_ID`,`aux_page_name`,`aux_page_text_type`,`aux_page_text`,`meta_keywords`,`meta_description`) VALUES (NULL,$args[1],$args[2],$args[3],$args[4],$args[5])");

} else
{
dbQuery("UPDATE `SC_aux_pages` SET aux_page_name_ru='" . $args[1] . "', aux_page_text_type='" . $args[2] .  "', aux_page_text='" . $args[3] . "', meta_keywords='" . $args[4] . "', meta_description_ru='" . $args[5] . "' WHERE aux_page_ID='" . $args[0]. "'");
}
}

function delAuxPage($args) {
dbQuery("DELETE  FROM `SC_aux_pages` WHERE aux_page_ID='" . $args . "'");
return true;
}


function getPicture($args) {
 if($img = @file_get_contents($args))
   {
      return new IXR_Base64($val);
   }

return new IXR_Error(-1, 'You did not open picture');

}
function setPicture($args) {
//return   mysql_resultTo2DAssocArray( dbQuery("SELECT *  FROM `SC_aux_pages` "));
}


function getTime($args) { 
        return "[" . $args . "]"; 
} 

function add($args) {
return $args[0];
//    return $args[0] + $args[1];
}

function addArray($array) {
    $total = 0;
    foreach ($array as $number) {
        $total += $number;
    }
    return implode(' + ', $array).' = '.$total;
}  

function addProduct($array) {
    $str = "";
       dump(sizeof($array));
$str   = "INSERT INTO `SC_products` (
`productID` ,
`categoryID` ,
`name_ru` ,
`description_ru` ,
`customers_rating` ,
`Price` ,
`in_stock` ,
`customer_votes` ,
`items_sold` ,
`enabled` ,
`brief_description_ru` ,
`list_price` ,
`product_code` ,
`sort_order` ,
`default_picture` ,
`date_added` ,
`date_modified` ,
`viewed_times` ,
`eproduct_filename` ,
`eproduct_available_days` ,
`eproduct_download_times` ,
`weight` ,
`meta_description_ru` ,
`meta_keywords_ru` ,
`free_shipping` ,
`min_order_amount` ,
`shipping_freight` ,
`classID`
)
VALUES (
NULL , '".$array[0]."' , '".$array[1]."', '".$array[2]."', '0', '".$array[3]."', '1', '0', '', '1', NULL , NULL , NULL , '0', NULL , NULL , NULL , '0', NULL , '5', '5', '0', NULL , NULL , '0', '1', '0', NULL
)";

dbQuery($str);
dump(mysql_error());
}  


function addCategory($array) {
       dump(($array));
$str   = "INSERT INTO `SC_categories` (
`categoryID`,
`parent`,
`products_count`,
`picture`,
`products_count_admin`,
`sort_order`,
`viewed_times`,
`allow_products_comparison`,
`allow_products_search`,
`show_subcategories_products`,
`name_en`,
`description_en`,
`meta_title_en`,
`meta_description_en`,
`meta_keywords_en`,
`slug`,
`name_ru`,
`description_ru`,
`meta_title_ru`,
`meta_description_ru`,
`meta_keywords_ru`,
`vkontakte_type`,
`id_1c`
)
VALUES ( NULL , '".$array[0]."' , NULL , NULL, NULL, 0, 0, 1, 1, 1, '', '', '', '', '',
 'products', '".$array[1]."', '".$array[2]."', '', '', '', 0, '".$array[3]."' )";

dbQuery($str);
dump(mysql_error());
} 



$server = new IXR_Server(array(
'shopscript.getPicture' => 'getPicture',
'shopscript.getNews' => 'getNews',
'shopscript.setNewsNumber' => 'setNewsNumber',
'shopscript.delNews' => 'delNews',
'shopscript.setPicture' => 'setPicture',
'shopscript.getSQL' => 'getSQL',
'shopscript.getVersion' => 'getVersion',
'shopscript.getPayments' => 'getPayments',
'shopscript.getCategory' => 'getCategory',
'shopscript.getCategoryNumber' => 'getCategoryNumber',
'shopscript.setCategoryNumber' => 'setCategoryNumber',
'shopscript.getPaymentsNumber' => 'getPaymentsNumber',
'shopscript.getProducts' => 'getProducts',
'shopscript.getProductNumber' => 'getProductNumber',
'shopscript.setProductNumber' => 'setProductNumber',
'shopscript.addProduct' => 'addProduct',
'shopscript.addCategory' => 'addCategory',
'shopscript.getAuxPage' => 'getAuxPage',
'shopscript.setAuxPageNumber' => 'setAuxPageNumber',
'shopscript.delAuxPage' => 'delAuxPage',
'test.add' => 'add',
'test.addArray' => 'addArray',
'shopscript.getTime' => 'getTime'));
?>